from manim import *



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        circle1 = Circle(radius=1, color=RED,fill_opacity=0.5)  
        circle1.move_to([-47.5,27.5,0]) 
        circle2 = Circle(radius=1, color=YELLOW,fill_opacity=0.5)  
        circle2.move_to([-42.5,27.5,0])  
        circle3 = Circle(radius=1, color=GREEN,fill_opacity=0.5) 
        circle3.move_to([-37.5,27.5,0])
        #镜头跟进效果
        self.camera.frame.save_state()
         #分析文本
        text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-35,7.5,0])
        self.play(Write(text100))
        #题目出现
        text = Tex(r"$ \int _{0}^{a}\sqrt{2y^{2}+a^{2}}dy$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,20,0])
        self.play(Write(text),self.camera.frame.animate.scale(0.8).move_to([0,20,0]))
        text1 = Tex(r"$ =\dfrac{a^{2}}{\sqrt{2}}\int _{0}^{\arctan \sqrt{2}}\sec ^{3}tdt$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,15,0])
        text2 = Tex(r"$ =\dfrac{a^{2}}{2\sqrt{2}}(\sec t\tan t+\ln \left| \sec t+\tan t\right|)| _{0}^{\arctan \sqrt{2}}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,10,0])
        self.play(Write(text1),Write(text2))
        #透明框框
        rectangle=Rectangle(color=YELLOW,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([0,10,0])
        self.play(
            rectangle.animate.stretch(45.0,dim=0)
        )

        
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(circle1),run_time=0.3)
        self.play(FadeIn(circle2),run_time=0.3)
        self.play(FadeIn(circle3),run_time=0.3)


        text3 = Tex(r"$ \tan \left( \arctan \sqrt{2}\right) $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,3,0])
        text4 = Tex(r"$ \tan \left( 0\right) $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-2,0])
        text5 = Tex(r"$ \sec \left( 0\right) $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-7,0])
        text6 = Tex(r"$ \sec \left( \arctan \sqrt{2}\right) $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-13,0])
        
        self.play(Write(text3),Write(text4),Write(text5),Write(text6))


        text7 = Tex(r"$ =\sqrt{2} $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text3,RIGHT,buff=2)
        text8 = Tex(r"$=0 $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text4,RIGHT,buff=2)
        text9 = Tex(r"$=1$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text5,RIGHT,buff=2)
        text10 = Tex(r"$ =? $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text6,RIGHT,buff=2)
        
        self.play(Write(text7))
        self.play(Write(text8))
        self.play(Write(text9))
        self.play(Write(text10))
        

# Create three points of the right triangle
        A = np.array([20, -5, 0])
        B = np.array([32, -5, 0])
        C = np.array([20, 4, 0])

        line_AB = Line(A, B, stroke_width=20)  # Increase stroke width to 5
        line_BC = Line(B, C, stroke_width=20)
        line_CA = Line(C, A, stroke_width=20)

        # Group the lines to form the triangle
        triangle = VGroup(line_AB, line_BC, line_CA)

        label_A = Text("A", font_size=200).next_to(A, LEFT)  # Increase font size to 36
        label_B = Text("B", font_size=200).next_to(B, RIGHT)
        label_C = Text("C", font_size=200).next_to(C, UP)

        # Add the triangle and labels to the scene
        self.play(Create(triangle))
        self.play(Write(label_A), Write(label_B), Write(label_C))

        # Pause for a moment to display the triangle
        arc_B = Arc(radius=4, angle=PI/6, arc_center=B, start_angle=5*PI/6, color=BLUE,stroke_width=20)
        #文本
        text11 = Tex(r"$ t $",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-2.5,0])
        

        text12 = Tex(r"$\sqrt{x+1} $",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([27.5,2.5,0])
        text13 = Tex(r"$ 1 $",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(line_AB,DOWN,buff=1)
        text14 = Tex(r"$ \sqrt{x} $",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(line_CA,LEFT,buff=1)
        self.play(Create(arc_B),Create(text11),Create(text12),Create(text13),Create(text14))





        text15 = Tex(r"$ \tan t=\sqrt{x} $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-15,0])
        text16 = Tex(r"$\sin t=\dfrac{\sqrt{x}}{\sqrt{x+1}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,-10,0])
        text17 = Tex(r"$\cos t=\dfrac{1}{\sqrt{x+1}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,-20,0])
        self.play(Create(text15))
        self.play(Create(text16))
        self.play(Create(text17))
        text18 = Tex(r"$ t=\arctan \sqrt{x} $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-15,0])
        text19 = Tex(r"$t=\arcsin \sqrt{\dfrac{x}{x+1}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,-10,0])
        text20 = Tex(r"$t=\arccos \dfrac{1}{\sqrt{x+1}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,-20,0])
        
        #丝滑变化
        self.play(
            ReplacementTransform(text15,text18),
            ReplacementTransform(text16,text19),
            ReplacementTransform(text17,text20)

        )
        self.wait(0.5)



        #框框标记


        rectangle1=Rectangle(color=BLUE,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=BLUE,stroke_width=0).move_to([17,-15,0])
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=BLUE,stroke_width=0).move_to([38,-10,0])
        rectangle3=Rectangle(color=BLUE,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=BLUE,stroke_width=0).move_to([38,-20,0])
        self.play(
            rectangle1.animate.stretch(12.0,dim=0),
            rectangle2.animate.stretch(16.0,dim=0),
            rectangle3.animate.stretch(16.0,dim=0)
        )

        #？消除
        self.play(FadeOut(text10))

        text21 = Tex(r"$ \sec \left( \arccos \dfrac{1}{\sqrt{3}}\right)  $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-13,0])
        text22 = Tex(r"$ \dfrac{1}{\cos \left( \arccos \dfrac{1}{\sqrt{3}}\right) }=\sqrt{3} $"
                   ,color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-13,0])
        self.play(
            ReplacementTransform(text6,text21)

        )
        self.play(
            ReplacementTransform(text21,text22)

        )

        #框框

        rectangle4=Rectangle(color=BLUE,fill_opacity=0.0,width=35,height=23
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle4.move_to([-15, -6, 0])
        self.play(Create(rectangle4))

        text23 = Tex(r"$=\left( \dfrac{\sqrt{3}}{2}+\dfrac{\ln \left( \sqrt{3}+\sqrt{2}\right) }{2\sqrt{2}}\right) a^{2}$"
                   ,color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle,RIGHT,buff=3)
        
        self.play(Create(text23),self.camera.frame.animate.move_to([40,10,0]))



        self.wait(0.3)






        self.play(Restore(self.camera.frame))

        self.wait()